package cn.iocoder.yudao.module.mon_platform.controller.admin.ys7deviceinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mon_platform.controller.admin.ys7deviceinfo.vo.*;
import cn.iocoder.yudao.module.mon_platform.convert.ys7deviceinfo.Ys7DeviceInfoConvert;
import cn.iocoder.yudao.module.mon_platform.dal.dataobject.ys7deviceinfo.Ys7DeviceInfoDO;
import cn.iocoder.yudao.module.mon_platform.service.ys7deviceinfo.Ys7DeviceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 监控-萤石设备信息")
@RestController
@RequestMapping("/mon_platform/ys7")
@Validated
public class Ys7DeviceInfoController {

    @Resource
    private Ys7DeviceInfoService ys7DeviceInfoService;

    @PostMapping("/create")
    @Operation(summary ="创建监控-萤石设备信息")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:create')")
    public CommonResult<Long> createYs7DeviceInfo(@Valid @RequestBody Ys7DeviceInfoCreateReqVO createReqVO) {
        return success(ys7DeviceInfoService.createYs7DeviceInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新监控-萤石设备信息")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:update')")
    public CommonResult<Boolean> updateYs7DeviceInfo(@Valid @RequestBody Ys7DeviceInfoUpdateReqVO updateReqVO) {
        ys7DeviceInfoService.updateYs7DeviceInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除监控-萤石设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:delete')")
    public CommonResult<Boolean> deleteYs7DeviceInfo(@RequestParam("id") Long id) {
        ys7DeviceInfoService.deleteYs7DeviceInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得监控-萤石设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:query')")
    public CommonResult<Ys7DeviceInfoRespVO> getYs7DeviceInfo(@RequestParam("id") Long id) {
        Ys7DeviceInfoDO ys7DeviceInfo = ys7DeviceInfoService.getYs7DeviceInfo(id);
        return success(Ys7DeviceInfoConvert.INSTANCE.convert(ys7DeviceInfo));
    }

    @GetMapping("/list")
    @Operation(summary ="获得监控-萤石设备信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:query')")
    public CommonResult<List<Ys7DeviceInfoRespVO>> getYs7DeviceInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<Ys7DeviceInfoDO> list = ys7DeviceInfoService.getYs7DeviceInfoList(ids);
        return success(Ys7DeviceInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得监控-萤石设备信息分页")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:query')")
    public CommonResult<PageResult<Ys7DeviceInfoRespVO>> getYs7DeviceInfoPage(@Valid Ys7DeviceInfoPageReqVO pageVO) {
        PageResult<Ys7DeviceInfoDO> pageResult = ys7DeviceInfoService.getYs7DeviceInfoPage(pageVO);
        PageResult<Ys7DeviceInfoRespVO> ys7DeviceInfoRespVOPageResult = Ys7DeviceInfoConvert.INSTANCE.convertPage(pageResult);


        return success(ys7DeviceInfoRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出监控-萤石设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('mon_platform:ys7:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportYs7DeviceInfoExcel(@Valid Ys7DeviceInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<Ys7DeviceInfoDO> list = ys7DeviceInfoService.getYs7DeviceInfoList(exportReqVO);
        // 导出 Excel
        List<Ys7DeviceInfoExcelVO> datas = Ys7DeviceInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "监控-萤石设备信息.xls", "数据", Ys7DeviceInfoExcelVO.class, datas);
    }


}
